/** 
 * Copyright 2014 jingzhe All Rights Reserved.
 * 项目名：jingzhe-core
 * 包名：com.jingzhe.core.web.converter
 * 文件名：Result.java 
 * 日期：2016-02-04 16:13
 */
package com.jingzhe.core.web.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jingzhe.core.constant.ErrorCodeConstant;


/**
 * 返回结果包装类.
 * @author wanghaijun
 *
 *@param <T>
 */
public class Result<T> {
    private boolean success = true;
    private int errorCode = ErrorCodeConstant.SUCCESS_CODE;
    private String msg = "操作成功";
    private Map<String,String> errorMap;
    private T data;

    public Result(T object) {
        if (object == null) {
            this.fail(ErrorCodeConstant.DATA_NOT_FOUND);
        } else {
            this.success = true;
            this.errorCode = ErrorCodeConstant.SUCCESS_CODE;
            this.msg = "操作成功";
            this.data = object;
        }
    }

    public void fail() {
        this.errorCode = ErrorCodeConstant.UNKNOW_ERROR_CODE;
        this.success = false;
        this.msg = "操作失败";
        this.data = null;
    }

    public void fail(int errorCode) {
        this.success = false;
        this.errorCode = errorCode;
        this.msg = "操作失败";
        this.data = null;
    }

    public void fail(int errorCode, String msg) {
        this.success = false;
        this.errorCode = errorCode;
        this.msg = msg;
        this.data = null;
        this.errorMap = null;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return this.success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * @return the errorCode
     */
    public int getErrorCode() {
        return this.errorCode;
    }

    /**
     * @param errorCode the errorCode to set
     */
    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return this.msg;
    }

    /**
     * @param msg the msg to set
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * @return the data
     */
    public T getData() {
        return this.data;
    }

    /**
     * @param data the data to set
     */
    public void setData(T data) {
        this.data = data;
        if(data != null){
            this.success = true;
            this.errorCode = ErrorCodeConstant.SUCCESS_CODE;
            this.msg = "操作成功";
        }
    }

    /**
     * @return the errorMap
     */
    public Map<String, String> getErrorMap() {
        return errorMap;
    }

    /**
     * @param errorMap the errorMap to set
     */
    public void setErrorMap(Map<String, String> errorMap) {
        this.errorMap = errorMap;
    }
}
